//
//  EmpyView.m
//  TanTe
//
//  Created by zyming on 2020/11/17.
//  Copyright © 2020 mo. All rights reserved.
//

#import "EmpyView.h"
#import "BaseToolsKit.h"

#define RGBFromHex(s) [UIColor colorWithRed:(((s & 0xFF0000) >> 16 )) / 255.0 green:((( s & 0xFF00 ) >> 8 )) / 255.0 blue:(( s & 0xFF )) / 255.0 alpha:1.0]
@implementation EmpyView

+ (void)emptyViewInView:(UIView *)view andData:(NSArray *)dataSource {
    
    if (dataSource && dataSource.count > 0) {
        for (UIView *subV in view.subviews) {
            if (subV.tag == 10086) {
                [subV removeFromSuperview];
            }
        }
    }else{
        for (UIView *subV in view.subviews) {
            if (subV.tag == 10086) {
                return;
            }
        }
        EmpyView *emptyView = [[EmpyView alloc] initWithFrame:view.bounds];
        emptyView.tag = 10086;
        [view addSubview:emptyView];
    }
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    
    self.backgroundColor = UIColor.whiteColor;
    
    UILabel *label = [self createUILabelWithBackgroudColor:UIColor.clearColor andNumberOfLines:1 andTextColor:RGBFromHex(0x111111) andFont:[UIFont systemFontOfSize:16] andTextAlignment:NSTextAlignmentCenter andText:GDBaselocalString(@"暂无数据")];
    [label sizeToFit];
    label.center = CGPointMake(self.mo_width/2, self.mo_height/2);
    
}

+ (void)emptyViewInView:(UIView *)view andData:(NSArray *)dataSource andEmptyImage:(UIImage *)emptyImage andEmptyTitle:(NSString *)emptyTitle{
    
    if (dataSource && dataSource.count > 0) {
        for (UIView *subV in view.subviews) {
            if (subV.tag == 10086) {
                [subV removeFromSuperview];
            }
        }
    }else{
        for (UIView *subV in view.subviews) {
            if (subV.tag == 10086) {
                return;
            }
        }
        EmpyView *emptyView = [[EmpyView alloc] initWithFrame:view.bounds andEmptyImage:emptyImage andEmptyTitle:emptyTitle];
        emptyView.tag = 10086;
        [view addSubview:emptyView];
    }
}
- (instancetype)initWithFrame:(CGRect)frame andEmptyImage:(UIImage *)emptyImage andEmptyTitle:(NSString *)emptyTitle {
    if (self = [super initWithFrame:frame]) {
        [self setupEmptyViewEmptyImage:emptyImage andEmptyTitle:emptyTitle];
    }
    return self;
}
- (void)setupEmptyViewEmptyImage:(UIImage *)emptyImage andEmptyTitle:(NSString *)emptyTitle{
    
    self.backgroundColor = RGBFromHex(0xFBFBFF);
    
    UILabel *remindLabel = [self createUILabelWithBackgroudColor:UIColor.clearColor andNumberOfLines:0 andTextColor:RGBFromHex(0x999999) andFont:[UIFont systemFontOfSize:14] andTextAlignment:NSTextAlignmentCenter andText:emptyTitle];
    remindLabel.width = self.width-40;
    [remindLabel sizeToFit];
    remindLabel.center = CGPointMake(self.mo_width/2, self.mo_height/2);
    
    
    if (emptyImage) {
        UIImageView *emptyImageView = [self createUIImageViewWithBackgroudColor:UIColor.clearColor andContentMode:UIViewContentModeScaleToFill];
        [emptyImageView setImage:emptyImage];
        [emptyImageView sizeToFit];
        emptyImageView.centerX = self.width/2;
        emptyImageView.centerY = self.height/2-64;
        remindLabel.y = emptyImageView.maxY+20;
    }
    
}

@end
